package com.cyj.dream.auth.handler;


import com.cyj.dream.auth.util.ResultUtil;
import com.cyj.dream.core.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 用户登出
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.auth.handler
 * @Author: ChenYongJia
 * @CreateTime: 2021-10-08 13:07
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class UserLogoutSuccessHandler implements LogoutSuccessHandler {

    /**
     * 用户登出返回结果
     * 这里应该让前端清除掉Token
     *
     * @Author ChenYongJia
     * @CreateTime 2020-5-11 09:50:22
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        log.info("-----------进入用户登出返回结果处理器-----------");
        Map<String, Object> resultData = new HashMap<>(Constant.INITIAL_CAPACITY);
        resultData.put("code", 200);
        resultData.put("message", "登出成功");
        SecurityContextHolder.clearContext();
        ResultUtil.responseJson(response, ResultUtil.resultSuccess(resultData));
    }

}
